package com.li.commons.util;

import java.io.File;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;

public class MailUtil{
	
	private final static String HOST_NAME="smtp.exmail.qq.com";
	private final static String SMTP_PORT="465";
	private final static String FROM="yunying2@ikongjian.com";
	private final static String PASSWORD="space1234567";
	
	public static void sendSimpleEmail(String[] toes,String[] cces,String subject,String msg){
		Email email = new SimpleEmail();
		email.setHostName(HOST_NAME);
		email.setSslSmtpPort(SMTP_PORT);
		email.setAuthenticator(new DefaultAuthenticator(FROM,PASSWORD));
		email.setSSLOnConnect(true);
		try {
			email.setFrom(FROM,"爱空间官网运营");
			email.setSubject(subject);
			email.setMsg(msg);
			for(String to:toes){
				email.addTo(to);
			}
			for(String cc:cces){
				email.addTo(cc);
			}
			email.setSendPartial(true);
			email.send();
		} catch (EmailException e) {
			e.printStackTrace();
		}
	}
	
	public static void sendHtmlEmail(String[] toes,String[] cces,String subject,String msg) throws Exception{
		  HtmlEmail htmlEmail = new HtmlEmail();
		  htmlEmail.setHostName(HOST_NAME);
		  htmlEmail.setSslSmtpPort(SMTP_PORT);
		  htmlEmail.setAuthenticator(new DefaultAuthenticator(FROM,PASSWORD));
		  htmlEmail.setSSLOnConnect(true);
		  htmlEmail.setFrom(FROM,"爱空间官网运营");
		  for(String to:toes){
			  htmlEmail.addTo(to);
		  }
		  for(String cc:cces){
			  htmlEmail.addTo(cc);
			}
		  htmlEmail.setSendPartial(true);
		  htmlEmail.setSubject(subject);
		  // embed the image and get the content id
//		  URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
		  //String cid = htmlEmail.embed(url, "Apache logo");
		  // set the html message
		  htmlEmail.setHtmlMsg(msg);
		  // set the alternative message
		  //htmlEmail.setTextMsg("Your email client does not support HTML messages");
		  // send the email
		  htmlEmail.send();
	}
	
	
	public static void sendMutipartEmail(String from, String[] toes,String[] cces,String subject,String msg,String attachmentPath){
			MultiPartEmail  email = new MultiPartEmail();       
			email.setHostName(HOST_NAME);
			email.setSslSmtpPort(SMTP_PORT);
			email.setAuthenticator(new DefaultAuthenticator(FROM,PASSWORD));
			email.setSSLOnConnect(true);
	        try{    
			   EmailAttachment attachment = new EmailAttachment();  
			   //绝对路径  
			   File attachmentFile = new File(attachmentPath);
			   attachment.setPath(attachmentPath);
			   attachment.setDisposition(EmailAttachment.ATTACHMENT);  
			   attachment.setName(attachmentFile.getName());  
			   email.setFrom(FROM, from);
			   //收件人
			   for(String to:toes){
				   email.addTo(to);
			   }
			   //抄送人
			   if(cces != null && cces.length != 0 ){
				   for(String cc:cces){
					   if(cc != null && !"".equals(cc.trim())){
						   email.addCc(cc); 
					   }
				   }
			   }
			   
			   email.setSendPartial(true);
			   email.setCharset("GB2312");//编码  
			   email.setSubject(subject);
			   email.setMsg(msg);
			   email.attach(attachment);  
			   email.send();  
	        } catch (EmailException e) {    
	            e.printStackTrace();    
	        }  
	}
	
	
	public static void main(String args[]){
		sendMutipartEmail("爱空间旧房抢购", "tangkf@ikongjian.com;huql@ikongjian.com;zhaochengju@163.com".split(";"),"tangkf@ikongjian.com;huql@ikongjian.com;zhaochengju@163.com".split(";"),"测试邮件","测试内容!","/Users/st/Documents/workspace/ikongjian/front/front-manage/src/main/resources/upload.properties");
	}
}